import { createRouter, createWebHashHistory } from "vue-router";
import type { MenuRouteInfo } from "@/types/type";
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { useUserInfoStore } from "@/store/userInfo";

export const routes: Array<MenuRouteInfo> = [
    {
        name: "Login",
        path: "/login",
        component: () => import("@/views/Login.vue"),
        meta: {
            showInAsideMenu: false,
        }
    },
    {
        name: "Register",
        path: "/register",
        component: () => import("@/views/Register.vue"),
        meta: {
            showInAsideMenu: false
        }
    },
    {
        name: "ResetPassword",
        path: "/resetPassword",
        component: () => import("@/views/ResetPassword.vue"),
        meta: {
            showInAsideMenu: false
        }
    },
    {

        name: "NotFound",
        path: "/404",
        component: () => import("@/views/NotFound.vue"),
        meta: {
            showInAsideMenu: false,
        }
    },
    {
        name: "404",
        path: '/:pathMatch(.*)*', // path: "/:catchAll(.*)", // VueRouter 4.x 写法
        redirect: "/404",
        meta: {
            showInAsideMenu: false,
        }
    },
    {
        name: "Main",
        path: "/",
        component: () => import("@/views/Main.vue"),
        redirect: "/home", // 根路径重定向到首页
        meta: {
            showInAsideMenu: true,
        },
        children: [
            {
                name: "Home",
                path: "home",
                component: () => import("@/views/Home.vue"),
                meta: {
                    title: "首页",
                    icon: "HomeFilled",
                    showInAsideMenu: true,
                }
            },
        ],
    },
    {
        name: "Apply",
        path: "/apply",
        component: () => import("@/views/Main.vue"),
        meta: {
            title: "我的申请",
            icon: "Avatar",
            showInAsideMenu: true,
        },
        redirect: "/apply/applying",
        children: [
            {
                name: "Applying",
                path: "/apply/applying",    // 子组件写全路径
                component: () => import("@/views/Applying.vue"),
                meta: {
                    title: "正在申请",
                    icon: "Promotion",
                    showInAsideMenu: true,
                },
            },
            {
                name: "Record",
                path: "/apply/applyRecord",
                component: () => import("@/views/ApplyRecord.vue"),
                meta: {
                    title: "申请记录",
                    icon: "Document",
                    showInAsideMenu: true,
                },
            },
        ]
    },
    {
        name: "Space",
        path: "/space",
        component: () => import("@/views/Main.vue"),
        meta: {
            title: "我的空间",
            icon: "Shop",
            showInAsideMenu: true,
        },
        redirect: "/space/manager",
        children: [
            {
                name: "Manager",
                path: "/space/manager",
                component: () => import("@/views/SpaceManager.vue"),
                meta: {
                    title: "空间管理",
                    icon: "Management",
                    showInAsideMenu: true,
                },
            },
            {
                name: "Status",
                path: "/space/status",
                component: () => import("@/views/SpaceStatus.vue"),
                meta: {
                    title: "空间状态",
                    icon: "List",
                    showInAsideMenu: true,
                },
            },
        ]
    },
    {
        name: "Admin",
        path: "/admin",
        component: () => import("@/views/Main.vue"),
        meta: {
            title: "管理员",
            icon: "Tools",
            showInAsideMenu: true,
        },
        redirect: "/admin/registerApproval",
        children: [
            {
                name: "RegisterApproval",
                path: "/admin/registerApproval",
                component: () => import("@/views/RegisterApproval.vue"),
                meta: {
                    title: "注册审批",
                    icon: "Message",
                    showInAsideMenu: true,
                },
            },
            {
                name: "ApplyApproval",
                path: "/admin/applyApproval",
                component: () => import("@/views/ApplyApproval.vue"),
                meta: {
                    title: "申请审批",
                    icon: "Finished",
                    showInAsideMenu: true,
                },
            },
        ]
    },
    {
        name: "About",
        path: "/about",
        component: () => import("@/views/Main.vue"),
        meta: {
            title: "关于",
            icon: "QuestionFilled",
            showInAsideMenu: true,
        },
        redirect: "/about/feedback",
        children: [
            {
                name: "Feedback",
                path: "/about/feedback",
                component: () => import("@/views/Feedback.vue"),
                meta: {
                    title: "反馈",
                    icon: "Comment",
                    showInAsideMenu: true,
                },
            },
            {
                name: "Version",
                path: "/about/version",
                component: () => import("@/views/Version.vue"),
                meta: {
                    title: "版本",
                    icon: "InfoFilled",
                    showInAsideMenu: true,
                },
            },
        ]
    },
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})


NProgress.configure({
	easing: 'ease', // 动画方式
	speed: 300, // 递增进度条的速度
	showSpinner: false, // 是否显示加载icon
	trickleSpeed: 200, // 自动递增间隔
	minimum: 0.3, // 更改启动时使用的最小百分比
	parent: 'body' //指定进度条的父容器
});


router.beforeEach((to: any, _from: any, next: any) => {
    const userInfo = useUserInfoStore()
    if (!userInfo.token 
        && !["/login",
            "/register",
            "/resetPassword"].includes(to.path)) {
        next({
            name: "Login"
        })
        return
    }
    NProgress.start()
    next()
})

router.afterEach(() => {
    NProgress.done()
})

export default router
